# -*- coding: utf-8 -*-
# @Author : zhaochen
# @Date   : 2019/1/22
# @File   : EG04_FrContributionRatioGetCnt.py
# @E-mail : zhaochen@bbdservice.com

'''eof
name:法人出资比例
code:EG04_FrContributionRatioGetCnt
tableName:
columnName:
groups:企业通用模块
dependencies:EG_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''

import sys
import re
import datetime
import numpy
reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def FormatData(tableName):
    try:
        eg = EG_DSJ.get("data").get("R1103V3")[0].get(tableName)
        return eg if isinstance(eg, list) else [{}]
    except:
        return [{}]



def float_check(target):
    try:
        float(target)
        return True
    except:
        return False



def EG04_FrContributionRatioGetCnt():
    try:
        null_type_list = ['', ' ',  None, 'null', 'Null', 'NULL', '/', '-', '--']
        shareHolderList = FormatData("shareHolderList")
        legalPerson = ratingRequest.get("legalPerson")

        try:
            basicList = FormatData("basicList")
            regCap = basicList[0].get("regCap")
            enterprise_type = basicList[0].get("enterpriseType")
        except:
            return u'缺失值'

        # 20190514新增逻辑，当为个人独资企业返回1
        if u"个人独资企业" == enterprise_type:
            return 1
        if shareHolderList == [{}]:
            return u'缺失值'
        else:
            if len(shareHolderList) >= 1:
                resultList = ''
                sumSubConam = 0
                currSubConam = 0
                resultPercent = 0
                nameTotal = 0
                flag = True
                for i in shareHolderList:
                    shareholderName = i.get("shareholderName")
                    fundedRatio = i.get("fundedRatio")
                    subConam = i.get("subConam")
                    if shareholderName not in null_type_list and u"公司" in shareholderName and len(shareholderName) > 5:
                        nameTotal += 1
                    if float_check(subConam):
                        sumSubConam += float(subConam)
                    if shareholderName not in null_type_list and shareholderName == legalPerson:
                        flag = False
                        currSubConam = subConam
                        if resultList in null_type_list and fundedRatio not in null_type_list:
                            resultList = fundedRatio

                # 所有股东名称全包含公司, 且法人不再其中
                if len(shareHolderList) == nameTotal:
                    return 0
                # 在所有股东名单里面， 未找到法人
                if flag:
                    return 0

                isPercent = "%" in resultList
                if isPercent:
                    percent = resultList.replace("%", "")
                    resultPercent = float(percent)/100.0 if float_check(percent) else u'缺失值'
                else:
                    if float_check(resultList):
                        resultPercent = float(resultList) if float(resultList) < 1 else float(resultList)/100.0

                if resultPercent > 0:
                    return '%.4f' % resultPercent
                else:
                    if float_check(regCap) and float(regCap) == sumSubConam and float_check(currSubConam) and currSubConam not in null_type_list:
                        return '%.4f' % (float(currSubConam)/float(sumSubConam))
                    return u'缺失值'
            else:
                # 不存在股东股本信息
                return 0
    except:
        return u'缺失值'


result = EG04_FrContributionRatioGetCnt()

